package com.dt.app.controller.product;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.ArrayList;
import com.dt.app.common.response.ResultUtil;
import com.dt.app.modules.product.entities.Category;
import com.dt.app.modules.product.vo.CategoryBean;
import com.dt.app.api.product.service.ICategoryService;
import com.dt.app.controller.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 鸿源塔基
 * @since 2024-03-05
 */
@RestController
@RequestMapping("api/v1/product/category")
public class CategoryController extends BaseController{
    @Autowired
    private ICategoryService categoryService;

    @ApiOperation(value = "列表")
    @RequestMapping("list")
    public ResultUtil<List<CategoryBean>> list(CategoryBean request){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        boolean isSh=false;
        if(!StringUtils.isEmpty(request.getCname())){
            wrapper.like("cname",request.getCname());
            isSh=true;
        }
        if(!StringUtils.isEmpty(request.getCno())){
            wrapper.eq("cno",request.getCno());
            isSh=true;
        }
        wrapper.eq("company_id",this.getSysCompanyId());
        wrapper.orderByDesc("seq");
        List<Category> entityList= categoryService.list(wrapper);
        List<CategoryBean> result=null;
        if(entityList!=null && !entityList.isEmpty()) {
            List<CategoryBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                CategoryBean bean = new CategoryBean();
                BeanUtils.copyProperties(it,bean);
                responseList.add(bean);
            });
            if(!isSh){//搜索
                result = categoryService.buildTree(responseList,0);
            }else{
                responseList.forEach(it -> {
                    it.setPathName(categoryService.getPathName(it.getParentId()));
                });
                result=responseList;
            }
        }
        return ResultUtil.success(result);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增")
    public ResultUtil save(@RequestBody @Valid Category entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getSeq()==null)entity.setSeq(1);
        entity.setCompanyId(this.getSysCompanyId());
        categoryService.save(entity);
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询")
    public ResultUtil<CategoryBean> getById(@PathVariable String id){
        Category entity = categoryService.getById(id);
        if(entity!=null){
            CategoryBean bean = new CategoryBean();
            BeanUtils.copyProperties(entity,bean);
            if(entity.getParentId().intValue()==0){
                bean.setParentName("/");
            }else{
                bean.setParentName(categoryService.getById(entity.getParentId()).getCname());
            }
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改")
    public ResultUtil update(@RequestBody @Valid Category entity){
        categoryService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        categoryService.removeById(id);
        return ResultUtil.success();
    }
}